<%@ page import="fi.hell.lunchit.Round" %>
<%@ page import="fi.hell.lunchit.Vote" %>
<%@ page import="fi.hell.lunchit.User" %>
<%@ page import="fi.hell.lunchit.Restaurant" %>

<g:set var="locale" value="${grailsApplication.config.locale}" />

<div class="votingResults" class="dialog">
	<h2><g:message code="todaysResults" default="Today's results" /></h2>
	
	<div class="statBox">
	    <h3><g:message code="userSelections" default="User selections" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Round.userSelections()}">
	        	<li class="controller"><g:link controller="user" action="show" id="${r.user?.id}">${r.user}</g:link> -> <g:link controller="restaurant" action="show" id="${r.selection.id}">${r.selection}</g:link></li>
	        </g:each>
	    </ul>
    </div>

    <div class="statBox">
	    <h3><g:message code="userVotes" default="User votes" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Round.currentRounds()}">
                <g:each var="v" in="${r.votes.sort({it.vote}).reverse()}">
                  <li class="controller"><g:link controller="user" action="show" id="${r.user?.id}">${r.user}</g:link> - <g:link controller="restaurant" action="show" id="${v.restaurant.id}">${v.restaurant}</g:link> - ${v.vote} <g:message code="points" default="points" /></li>
                </g:each>
	        </g:each>
	    </ul>
    </div>

	<div class="statBox">
    	<h3><g:message code="topSelections" default="Top selections" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Round.roundTopSelections()}">
	        	<li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="selections" default="selection(s)" /></li>
	        </g:each>
	    </ul>
    </div>
</div>

<g:if test="${loggedInUser}">
  <div class="votingResults" class="dialog">
      <h2><g:message code="yourStatistics" default="Your statistics" /></h2>

      <div class="statBox">
          <h3><g:message code="yourFavouriteSelections" default="Your favourite selections" /></h3>
          <ul class="roundResults">
              <g:each var="r" in="${loggedInUser.favouritRestaurants()}">
                  <li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="times" default="times" /></li>
              </g:each>
          </ul>
      </div>

      <div class="statBox">
          <h3><g:message code="mostVotedRestaurants" default="Most voted restaurants" /></h3>
          <ul class="roundResults">
              <g:each var="r" in="${loggedInUser.mostVotedRestaurants()}">
                  <li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="points" default="points" /></li>
              </g:each>
          </ul>
      </div>

      <div class="statBox">
          <h3><g:message code="totalMoneyUsed" default="Total money used" /></h3>
          <g:set var="totalMoneyUsed" value="${loggedInUser.moneyUsed()/100}" />
          <p><g:formatNumber number="${totalMoneyUsed}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
      </div>

      <div class="statBox">
          <h3><g:message code="avgCostPerLunch" default="Average cost per lunch" /></h3>
          <g:if test="${totalMoneyUsed > 0}">
            <p><g:formatNumber number="${totalMoneyUsed/Round.countByUserAndPriceIsNotNull(loggedInUser)}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
          </g:if>
          <g:else>
            <p><g:formatNumber number="${0}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
          </g:else>
      </div>
      <div class="clear"></div>

      <div class="statBox">
          <h3><g:message code="moneySavedWithLuncheonVouchers" default="Money saved with luncheon vouchers" /></h3>
          <p><g:formatNumber number="${loggedInUser.luncheonVoucherSavings()/100}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
      </div>
  </div>
</g:if>

<div class="votingResults" class="dialog">
	<h2><g:message code="restaurantStats" default="Restaurant statistics" /></h2>
	
	<div class="statBox">
	    <h3><g:message code="heavyUsers" default="Heavy users" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Restaurant.list()}">
	        	<g:set var="heavyUser" value="${r.heavyUser()}" />
	        	<g:if test="${heavyUser}">
	        		<li class="controller"><g:link controller="restaurant" action="show" id="${r.id}">${r}</g:link> - <g:link controller="user" action="show" id="${heavyUser.user?.id}">${heavyUser.user}</g:link> - ${heavyUser.count} <g:message code="times" default="times" /></li>
	        	</g:if>
	        </g:each>
	    </ul>
	</div>
	
	<div class="statBox">
	    <h3><g:message code="heavyVoters" default="Heavy voters" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Restaurant.list()}">
	        	<g:set var="heavyVoter" value="${r.heavyVoter()}" />
	        	<g:if test="${heavyVoter}">
	        		<li class="controller"><g:link controller="restaurant" action="show" id="${r.id}">${r}</g:link> - <g:link controller="user" action="show" id="${heavyVoter.user?.id}">${heavyVoter.user}</g:link> - ${heavyVoter.sum} <g:message code="points" default="points" /></li>
	        	</g:if>
	        </g:each>
	    </ul>
	</div>

    <div class="statBox">
	    <h3><g:message code="mostVotedLeastSelected" default="Most voted, least selected" /></h3>
	    <ul class="roundResults">
	        <g:each var="item" in="${Restaurant.voteSelectionRatioList('desc')}">
              <g:if test="${item.value > 0}">
                <li class="controller"><g:link controller="restaurant" action="show" id="${item.key.id}">${item.key}</g:link> - <g:formatNumber number="${item.value}" type="number" maxFractionDigits="2" roundingMode="HALF_UP" locale="${locale}" /> <g:message code="votesNeededForSelection" default="votes needed for a selection" /></li>
              </g:if>
	        </g:each>
	    </ul>
	</div>

    <div class="statBox">
	    <h3><g:message code="popularByDayOfWeek" default="Most popular by day of week" /></h3>
	    <ul class="roundResults">
	        <g:each var="item" in="${Restaurant.popularByDay()}">
                <li class="controller">
                  <g:message code="day.${item.key}" />
                  <g:if test="${item.value}"><g:link controller="restaurant" action="show" id="${item.value.id}">${item.value}</g:link></g:if>
                  <g:else><g:message code="noSelections" default="No selections" /></g:else>
                </li>
	        </g:each>
	    </ul>
	</div>
</div>

<div class="votingResults" class="dialog">
	<h2><g:message code="allTimeStatistics" default="All time statistics" /></h2>
	
	<div class="statBox">
	    <h3><g:message code="userSelections" default="Top votes" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Vote.topVotes()}">
	        	<li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="points" default="points" /></li>
	        </g:each>
	    </ul>
    </div>
    
    <div class="statBox">
	    <h3><g:message code="topSelections" default="Top selections" /></h3>
	    <ul class="roundResults">
	        <g:each var="r" in="${Round.topSelections()}">
	        	<li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="selections" default="selection(s)" /></li>
	        </g:each>
	    </ul>
    </div>
    
    <div class="statBox">    
	    <h3><g:message code="prodigals" default="Most prodigal users" /></h3>
	    <g:each in="${User.totalExpenditures()}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - <g:formatNumber number="${user.value/100}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
        </g:each>
    </div>

    <div class="statBox">
	    <h3><g:message code="prudents" default="Most prudent users" /></h3>
	    <g:each in="${User.totalExpenditures(5, 'asc')}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - <g:formatNumber number="${user.value/100}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
        </g:each>
    </div>
    <div class="clear"></div>
    
    <div class="statBox">    
	    <h3><g:message code="prodigalAvg" default="Prodigals by average expenditure" /></h3>
	    <g:each in="${User.averageExpenditures()}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - <g:formatNumber number="${user.value/100}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
        </g:each>
    </div>

    <div class="statBox">
	    <h3><g:message code="prudentsAvg" default="Prudents by average expenditure" /></h3>
	    <g:each in="${User.averageExpenditures(5, 'asc')}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - <g:formatNumber number="${user.value/100}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
        </g:each>
    </div>
    
    <div class="statBox">    
	    <h3><g:message code="burgerKing" default="Burger kings" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('BURGER', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="burgers" default="burgers" /></p>
        </g:each>
    </div>
    
    <div class="statBox">    
	    <h3><g:message code="orientalOrals" default="Oriental orals" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('ORIENTAL', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="times" default="times" /></p>
        </g:each>
    </div>
    <div class="clear"></div>

    <div class="statBox">
	    <h3><g:message code="pizzaMobster" default="Pizza mobsters" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('PIZZA', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="pizzas" default="pizzas" /></p>
        </g:each>
    </div>
    
    <div class="statBox">
	    <h3><g:message code="busyBusinessMen" default="Busy business men" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('FASTFOOD', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="fast food meals" default="fast food meals" /></p>
        </g:each>
    </div>

    <div class="statBox">
	    <h3><g:message code="heavyEater" default="Heavy eater" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('BUFFET', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="buffets" default="buffets" /></p>
        </g:each>
    </div>

    <div class="statBox">
	    <h3><g:message code="homely" default="Homelies" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('HOMELY', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="homely lunches" default="homely lunches" /></p>
        </g:each>
    </div>
    <div class="clear"></div>
    
    <div class="statBox">
	    <h3><g:message code="kebab" default="Kebab" /></h3>
	    <g:each in="${User.mostSelectionsByTagName('KEBAB', 2)}" var="user">
          <p><g:link controller="user" action="show" id="${user.key.id}">${user.key}</g:link> - ${user.value} <g:message code="kebabs" default="kebabs" /></p>
        </g:each>
    </div>

    <div class="statBox">
	    <h3><g:message code="versatileUsers" default="Most versatile users" /></h3>
        <g:each var="item" in="${User.mostVersatileUsers()}">
            <p><g:link controller="user" action="show" id="${item.key.id}">${item.key}</g:link> - ${item.value} <g:message code="differentRestaurants" default="different restaurants" /></p>
        </g:each>
	</div>

    <g:if test="${loggedInUser?.company?.luncheonVoucherProfitValue > 0}">
      <div class="statBox">
          <h3><g:message code="voucherGrannys" default="Voucher grannys" /></h3>
          <g:each var="item" in="${User.luncheonVoucherUsers(5, 'desc')}">
              <p><g:link controller="user" action="show" id="${item.key.id}">${item.key}</g:link> - ${item.value} <g:message code="vouchers" default="vouchers" /></p>
          </g:each>
      </div>
    </g:if>
</div>